package com.zozo.guanli.dao.peijian;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zozo.guanli.dao.BaseDao;
import com.zozo.guanli.util.Page;
import com.zozo.guanli.vo.Accessory;

@Repository
public class PeijianDao extends BaseDao {

    /**
     * 查找
     * @param page
     * @param condition
     * @return
     */
    public Page<Accessory> findByPage(Page<Accessory> page, String sql) {
        page.setTotal(getTotal(sql));
        StringBuilder hql = new StringBuilder(" from Accessory a where 1=1 ");
        hql.append(sql);
        @SuppressWarnings("unchecked")
        List<Accessory> list = getSession().createQuery(hql.toString()).setFirstResult(page.getFirstResultNo()).setMaxResults(page.getPageSize()).list();
        page.setRows(list);
        return page;
    }

    public int getTotal(String sql) {
        StringBuilder hql = new StringBuilder(" select count(*) from Accessory a where 1=1 ");
        hql.append(sql);
        Query query = this.getSession().createSQLQuery(hql.toString());
        return ((BigInteger) query.list().iterator().next()).intValue();
    }
    
    
}
